package iwpl.project.servlet;

import iwpl.project.dao.EmployeesDAO;
import iwpl.project.entities.Employees;
import iwpl.project.entities.Offices;
import iwpl.project.entitymanagerhelper.EntityManagerHelper;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xerces.internal.impl.dtd.models.DFAContentModel;

public class EMPLOYEE_Servlet extends HttpServlet {
	

	/**
	 * Constructor of the object.
	 */
	public EMPLOYEE_Servlet() {
		super();
	}

	
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
		
			String action = request.getParameter("action");
			String empID=request.getParameter("id");
			Employees emp=new Employees();
			EmployeesDAO dao=new EmployeesDAO();
			if ("delete".equalsIgnoreCase(action)) {
				EntityManagerHelper.beginTransaction();
				emp.setId(Integer.parseInt(empID));
				dao.delete(emp);
				EntityManagerHelper.commit();
				}			
		response.sendRedirect("/DepartmentManage/Manage_Employee.jsp");
		//response.sendRedirect("/DepartmentManage/Manage_Employee.jsp");
		//doPost(request, response);
		/*SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
		String action = request.getParameter("action");
		String empID=request.getParameter("txtID");
		String fullName=request.getParameter("txtEmp_Name");
		String phone=request.getParameter("txtPhone");
		String sex=request.getParameter("chkSex");
		String birthdate=request.getParameter("txtBirthDate");
		String address=request.getParameter("txtAddress");
		String office=request.getParameter("cboOffice");
		if (sex == null)
			sex = "false";
		if (sex.equalsIgnoreCase("on"))
			sex = "true";
		Date date = null;
		try {
			date = sdf.parse(birthdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Timestamp ts=new Timestamp(date.getTime());
		Employees emp=new Employees();
		EmployeesDAO dao=new EmployeesDAO();
		emp.setFullName(fullName);
		emp.setAddress(address);
		emp.setPhone(phone);
		emp.setBirthday(ts);
		emp.setSex(sex);
		Offices officeObj=new Offices();
		officeObj.setId(Integer.parseInt(office));		
		emp.setOffices(officeObj);
		if(empID==null)
		{
			EntityManagerHelper.beginTransaction();
			dao.save(emp);
			EntityManagerHelper.commit();
		}
		else if(empID!=null)
		{
			EntityManagerHelper.beginTransaction();
			emp.setId(Integer.parseInt(empID));
			dao.update(emp);
			EntityManagerHelper.commit();
		}
		else if ("delete".equalsIgnoreCase(action)) {
			EntityManagerHelper.beginTransaction();
			emp.setId(Integer.parseInt(empID));
			dao.delete(emp);
			EntityManagerHelper.commit();
			}
		response.sendRedirect("/DepartmentManage/Manage_Employee.jsp");*/
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
		//String action = request.getParameter("action");
		String empID=request.getParameter("txtID");
		String fullName=request.getParameter("txtEmp_Name");
		String phone=request.getParameter("txtPhone");
		String sex=request.getParameter("chkSex");
		String birthdate=request.getParameter("txtBirthDate");
		String address=request.getParameter("txtAddress");
		String office=request.getParameter("cboOffice");
		if (sex == null)
			sex = "false";
		if (sex.equalsIgnoreCase("on"))
			sex = "true";
		Date date = null;
		try {
			date = sdf.parse(birthdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		Timestamp ts=new Timestamp(date.getTime());		
		Employees emp=new Employees();
		EmployeesDAO dao=new EmployeesDAO();		
		emp.setFullName(fullName);
		emp.setAddress(address);
		emp.setPhone(phone);
		emp.setBirthday(ts);
		emp.setSex(sex);
		Offices officeObj=new Offices();
		officeObj.setId(Integer.parseInt(office));
		
		if(empID==null)
		{
			EntityManagerHelper.beginTransaction();	
			emp.setOffices(officeObj);
			dao.save(emp);
			EntityManagerHelper.commit();
		}
		else
		{
			EntityManagerHelper.beginTransaction();			
			emp.setId(Integer.parseInt(empID));	
			emp.setOffices(officeObj);
			dao.update(emp);
			EntityManagerHelper.commit();
		}
		/*if ("delete".equalsIgnoreCase(action)) {
			EntityManagerHelper.beginTransaction();
			emp.setId(Integer.parseInt(empID));
			dao.delete(emp);
			EntityManagerHelper.commit();
			}*/
		response.sendRedirect("/DepartmentManage/Manage_Employee.jsp");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	
}
